package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 该采集器适用于：两个shell命令返回两个采集值的情况
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-2-25 下午2:40:41          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class DoubleShellCollector extends Collector {
    private static final Logger logger = Logger.getLogger(DoubleShellCollector.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "result1", "result2" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "shell1", "shell2" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] props = this.getParamMeta();
        String shell1 = this.getParamMap().get("shell1");
        String shell2 = this.getParamMap().get("shell2");
        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        BufferedReader reader = null;
        String result1 = null;
        String result2 = null;
        try {
            reader = CommandReader.getReader(ip, shell1, 0, 0);
            result1 = reader.readLine();
            reader = CommandReader.getReader(ip, shell2, 0, 0);
            result2 = reader.readLine();
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[props.length];
            propValues[0] = ResPropertyValue.createPropValue(result1, props[0]);
            propValues[1] = ResPropertyValue.createPropValue(result2, props[1]);
            propertyValueX.setValues(propValues);
            list.add(propertyValueX);
        } catch (IOException e) {
            logger.error("failed to execute DoubleShellCollector", e);
            throw new CollectorException(e);
        }
        return list;
    }

}
